package lib;

public class SortingAndSearching {

	public static final int NOT_FOUND = -1;

	/**
	 * Return true if the target element is found in the given data array within min and max index
	 *
	 * @param data   the array to be searched
	 * @param min    the minimum index ( index has to be greater than 0 )
	 * @param max    the maximum index ( index has to be greater than 0 )
	 * @param target the target value that need to be found
	 * @return       true if the target value is found
	 */
	public static <T extends Comparable<? super T>> int linearSearch(T[] data, int min, int max, T target) {

		int index = min;
		int locationOfTarget = NOT_FOUND;

		if (min < 0 || max < 0) {
			throw new ArrayIndexOutOfBoundsException("Max and Min index can't be less than zero");
		}

		while (locationOfTarget == NOT_FOUND && index < max) {
			if (data[index].compareTo(target) == 0) {
				locationOfTarget = index;
			}
			index++;
		}
		return locationOfTarget;
	}
}
